package com.lgqz.himalayafm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.bean.RankEntity;
import com.lgqz.himalayafm.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class RankAdapter extends RecyclerView.Adapter<RankAdapter.ViewHolder> implements View.OnClickListener{
    private Context context;
    private List<RankEntity.ListEntity>list;
    private OnChildCallback callback;
    private RecyclerView recyclerView;

    public RankAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setCallback(OnChildCallback callback) {
        this.callback = callback;
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.item_ranking,parent,false);
        view.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(view);
        return holder;
       // return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_ranking,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.title.setText(list.get(position).getTitle());
        holder.title1.setText(list.get(position).getFirstKResults().get(0).getTitle());
        holder.title2.setText(list.get(position).getFirstKResults().get(1).getTitle());
        holder.img.setImageURI(list.get(position).getCoverPath());



    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void tianJia(Collection<? extends RankEntity.ListEntity> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public void onClick(View view) {
        if (recyclerView != null && callback != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            long id = getItemId(position);
            callback.onChildClick(position, id, list.get(position));
        }

    }


    public static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView title;
        private TextView title1;
        private TextView title2;
        private SimpleDraweeView img;


        public ViewHolder(View itemView) {
            super(itemView);
            title = ((TextView) itemView.findViewById(R.id.item_title));
            title1 = ((TextView) itemView.findViewById(R.id.item_title1));
            title2= ((TextView) itemView.findViewById(R.id.item_title2));
            img = (SimpleDraweeView) itemView.findViewById(R.id.item_img);
        }
    }
    public interface OnChildCallback {
        void onChildClick(int position, long id, RankEntity.ListEntity listEntity);
    }
}
